﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Alex.Library.IO
{
    /// <summary>
    /// 目录操作Helper类
    /// </summary>
    public sealed class DirectoryHelper
    {
        /// <summary>
        /// 拷贝目录下的文件
        /// </summary>
        /// <param name="srcdir">源目录路径</param>
        /// <param name="destdir">目标目录路径</param>
        /// <param name="includeSubDir">是否拷贝子目录</param>
        public static void FileCopy(string srcdir, string destdir, bool includeSubDir = true)
        {
            DirectoryInfo dir;
            FileInfo[] files;
            DirectoryInfo[] dirs;
            string tmppath;

            //determine     if     the     destination     directory     exists,     if     not     create     it     
            if (!Directory.Exists(destdir))
            {
                Directory.CreateDirectory(destdir);
            }

            dir = new DirectoryInfo(srcdir);
  
            if (!dir.Exists)
            {
                return;
            }

            //get     all     files     in     the     current     dir     
            files = dir.GetFiles();

            //loop     through     each     file     
            foreach (FileInfo file in files)
            {
                //create     the     path     to     where     this     file     should     be     in     destdir     
                tmppath = Path.Combine(destdir, file.Name);

                //copy     file     to     dest     dir     
                file.CopyTo(tmppath, true);
            }

            //cleanup     
            files = null;

            //if     not     recursive,     all     work     is     done     
            if (!includeSubDir)
            {
                return;
            }

            //otherwise,     get     dirs     
            dirs = dir.GetDirectories();

            //loop     through     each     sub     directory     in     the     current     dir     
            foreach (DirectoryInfo subdir in dirs)
            {
                //create     the     path     to     the     directory     in     destdir     
                tmppath = Path.Combine(destdir, subdir.Name);

                //recursively     call     this     function     over     and     over     again     
                //with     each     new     dir.     
                FileCopy(subdir.FullName, tmppath, includeSubDir);
            }

            //cleanup     
            dirs = null;

            dir = null;
        }
        /// <summary>
        /// 判断目录是否存在
        /// </summary>
        /// <param name="path">目录路径</param>
        /// <returns></returns>
        public static bool DirectoryExists(string path) 
        {
            return Directory.Exists(path);
        }
        /// <summary>
        /// 拷贝目录
        /// </summary>
        /// <param name="srcDir">源目录</param>
        /// <param name="tgtDir">目标目录</param>
        public static void CopyDirectory(string srcDir, string tgtDir)
        {
            DirectoryInfo source = new DirectoryInfo(srcDir);
            DirectoryInfo target = new DirectoryInfo(tgtDir);

            if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new Exception("父目录不能拷贝到子目录！");
            }

            if (!source.Exists)
            {
                throw new Exception("源目录路径不存在！");
            }

            if (!target.Exists)
            {
                target.Create();
            }

            FileInfo[] files = source.GetFiles();

            for (int i = 0; i < files.Length; i++)
            {
                File.Copy(files[i].FullName, target.FullName + @"\" + files[i].Name, true);
            }

            DirectoryInfo[] dirs = source.GetDirectories();

            for (int j = 0; j < dirs.Length; j++)
            {
                CopyDirectory(dirs[j].FullName, target.FullName + @"\" + dirs[j].Name);
            }
        }
    }
}
